package com.easy.controller;

import com.easy.bean.*;
import com.easy.dto.OrderWithItemsDTO;
import com.easy.service.IOrderService;
import com.easy.util.LayData;
import com.easy.util.Page;
import com.easy.util.Result;
import com.easy.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    IOrderService orderService;

    @RequestMapping("list")
    @ResponseBody
    public LayData getList(Page page, String text, HttpSession session) {
        System.out.println("进入orderlist方法");
        User user = (User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);

        // 检查用户是否登录
        if (user == null) {
            return new LayData(1, "用户未登录", 0, null);
        }

        // 确保page对象不为null，并且有合理的默认值
        if (page == null) {
            page = new Page();
            page.setLimit(10); // 设置默认每页显示10条
        }

        System.out.println("分页参数: start=" + page.getStart() + ", limit=" + page.getLimit());

        List<Order> list = orderService.getList(page, text, user.getId());
        int count = orderService.getCount(text, user.getId());
        System.out.println("user的值为"+user.getId());
        System.out.println("查询到订单数量: " + count);
        System.out.println("返回订单列表大小: " + (list != null ? list.size() : 0));

        return new LayData(0, "获取成功", count, list);
    }
    @RequestMapping("delete")
    @ResponseBody
    public int delete(int orderid, int userid){
        return orderService.delete(orderid,userid);
    }

    @RequestMapping("del")
    @ResponseBody
    public int del(int orderid){
        return orderService.del(orderid);
    }

    @RequestMapping("add")
    @ResponseBody
    public LayData add(HttpSession session, Order order){
        System.out.println("进入add方法");

        User user=(User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);

        System.out.println("当前userid="+user.getId());
        order.setUserid(user.getId());
        int result=orderService.add(order);
        return LayData.success(200,"添加成功",order.getOrderid());
    }

    @RequestMapping("edit")
    @ResponseBody
    public LayData edit(Order order, HttpSession session) {
        User user = (User)session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if (user == null) {
            return new LayData(1, "用户未登录", 0, null);
        }

        // 设置用户ID确保安全
        order.setUserid(user.getId());

        // 更新订单状态
        int result = orderService.edit(order);

        if (result > 0) {
            return LayData.success();
        } else {
            return new LayData(400, "更新订单失败", 0, null);
        }
    }

    @RequestMapping("payment")
    @ResponseBody
    public LayData payment(int orderid) {
        String code=orderService.payment(orderid);
        return LayData.success(200,"",code);
    }


    @RequestMapping("paymentsuccess")
    @ResponseBody
    public LayData paymentSuccess(@RequestParam HashMap map){
        System.out.println(map);
        orderService.paymentSuccess(map);
        return LayData.success(200,"");
    }


    @RequestMapping("getorderstatus")
    @ResponseBody
    public LayData getorderstatus(int orderid){
        Order order=orderService.getOrderByID(orderid);
        return LayData.success(200,"",order.getStatus());
    }


    @RequestMapping("getorderbyid")
    @ResponseBody
    public Order getOrderByID(int orderid){
        return orderService.getOrderByID(orderid);
    }

    @RequestMapping("list1")
    @ResponseBody
    public LayData getList(Page page, String text) {
        // 管理员查看所有用户信息，不需要用户ID限制
        List<Order> list = orderService.getList1(page, text);
        int count = orderService.getCount1(text);
        return new LayData(count, list);
    }


    @RequestMapping("change")
    @ResponseBody
    public int change(Order order) {
        return orderService.change(order);
    }

    @PostMapping("/createWithItems")
    @ResponseBody
    public Result createWithItems(@RequestBody OrderWithItemsDTO orderDTO, HttpSession session) {
        User user = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if (user == null) {
            return Result.error("请先登录");
        }
        
        try {
            // 调用服务层方法创建订单和订单项
            int orderId = orderService.createOrderWithItems(orderDTO, user.getId());
            return Result.success(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("创建订单失败: " + e.getMessage());
        }
    }
}
